/*
 * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum ingilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package cn.itenzo.common.validate;

import com.baidu.unbiz.fluentvalidator.Validator;
import com.baidu.unbiz.fluentvalidator.ValidatorContext;
import com.baidu.unbiz.fluentvalidator.ValidatorHandler;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * Created by ENZO on 2017/4/12.
 */
public class MoneyValidate extends ValidatorHandler<String> implements Validator<String> {


    @Override
    public boolean validate(ValidatorContext context, String s) {

        if(null == s || s.length() <= 0){
            context.addErrorMsg(String.format("请输入金额"));
            return false;
        }

        Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$"); // 判断小数点后2位的数字的正则表达式
        Matcher match = pattern.matcher(s);
        if (!match.matches()) {

            context.addErrorMsg(String.format("%s，不是有效金额",s));
            return false;
        }

        return true;
    }
}
